﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;


namespace ZyGame.Editor
{
    public abstract class GameEditorEntryPoint : EditorWindow
    {
        public static GameEditorEntryPoint focus { get; private set; }

        private bool isAwake = false;
        protected GameEditorToolbar toolbar;
        private Dictionary<Type, GameEditorChildWindow> childs = new Dictionary<Type, GameEditorChildWindow>();
        public GameEditorChildWindow current { get; private set; }
        private Vector2 _position = Vector2.zero;

        public void LoadToolbar<T>() where T : GameEditorToolbar, new()
        {
            toolbar = new T();
            toolbar.parent = this;
        }

        public void Register<T>() where T : GameEditorChildWindow
        {
            if (!childs.TryGetValue(typeof(T), out GameEditorChildWindow window))
            {
                window = Activator.CreateInstance<T>();
                window.parent = this;
                childs.Add(typeof(T), window);
            }
        }

        public T Show<T>() where T : GameEditorChildWindow
        {
            if (!childs.TryGetValue(typeof(T), out GameEditorChildWindow window))
            {
                Register<T>();
            }
            if (current is not null)
            {
                current.OnDisable();
            }
            current = childs[typeof(T)];
            current.OnEnable();
            return (T)window;
        }

        public T GetChildWindow<T>() where T : GameEditorChildWindow
        {
            if (!childs.TryGetValue(typeof(T), out GameEditorChildWindow window))
            {
                return default;
            }
            return (T)window;
        }

        public virtual void OnEvent(string evtName, object evtData = null)
        {
            foreach (var item in childs.Values)
            {
                item.OnEvent(evtName, evtData);
            }
        }

        public virtual void Awake()
        {
            isAwake = true;
        }

        public virtual void OnGUI()
        {
            if (!isAwake || toolbar == null)
            {
                Awake();
            }
            toolbar.OnGUI();
            _position = GUILayout.BeginScrollView(_position);
            current.OnGUI();
            GUILayout.EndScrollView();
            if (UnityEngine.Event.current.type == EventType.MouseDown)
            {
                GUIUtility.keyboardControl = -1;
                this.Repaint();
            }
        }

        public virtual void Update()
        {
            if (current == null)
            {
                return;
            }
            foreach (var item in childs.Values)
            {
                item.Update();
            }
        }

        public virtual void OnEnable()
        {
            focus = this;
            if (current is null)
            {
                return;
            }
            current.OnEnable();
        }

        public virtual void OnDisable()
        {
            if (current is null)
            {
                return;
            }
            current.OnEnable();
        }

        public virtual void OnDestroy()
        {
            foreach (var item in childs.Values)
            {
                item.OnDestroy();
            }
        }
    }
}
